#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <iomanip> // 用于格式化输出

using namespace std;

// 学生成绩结构体
struct Student {
    string name;
    string id;
    string course;
    float score;
};

vector<Student> students; // 存储学生成绩的容器

// 记录学生成绩
void recordScore() {
    Student student;
    cout << "===== 记录学生成绩 =====" << endl;
    cout << "请输入学生姓名：";
    cin >> student.name;
    cout << "请输入学生学号：";
    cin >> student.id;
    cout << "请输入课程名称：";
    cin >> student.course;
    cout << "请输入成绩（0-100）：";
    cin >> student.score;

    // 检查成绩是否合法
    if (student.score < 0 || student.score > 100) {
        cout << "错误：成绩必须在0到100之间！" << endl;
        return;
    }

    students.push_back(student);
    cout << "成绩已成功记录！" << endl;
}

// 查询学生成绩
void queryScore() {
    int choice;
    string input;
    cout << "===== 查询学生成绩 =====" << endl;
    cout << "请选择查询方式：" << endl;
    cout << "1. 按学生姓名查询" << endl;
    cout << "2. 按学生学号查询" << endl;
    cout << "3. 按课程名称查询" << endl;
    cout << "请输入选项序号：";
    cin >> choice;

    cout << "请输入查询内容：";
    cin >> input;

    bool found = false;
    for (const auto& student : students) {
        if ((choice == 1 && student.name == input) ||
            (choice == 2 && student.id == input) ||
            (choice == 3 && student.course == input)) {
            cout << "姓名：" << student.name << ", 学号：" << student.id
                 << ", 课程：" << student.course << ", 成绩：" << student.score << endl;
            found = true;
        }
    }

    if (!found) {
        cout << "未找到相关记录！" << endl;
    }
}

// 统计课程成绩
void statScore() {
    string course;
    cout << "===== 统计课程成绩 =====" << endl;
    cout << "请输入课程名称：";
    cin >> course;

    float totalScore = 0;
    int count = 0;
    float maxScore = -1;
    float minScore = 101;

    for (const auto& student : students) {
        if (student.course == course) {
            totalScore += student.score;
            count++;
            if (student.score > maxScore) maxScore = student.score;
            if (student.score < minScore) minScore = student.score;
        }
    }

    if (count == 0) {
        cout << "未找到该课程的成绩记录！" << endl;
    } else {
        cout << "课程：" << course << endl;
        cout << "平均分：" << fixed << setprecision(2) << totalScore / count << endl;
        cout << "最高分：" << maxScore << endl;
        cout << "最低分：" << minScore << endl;
    }
}

// 主菜单
void showMenu() {
    cout << "=================================" << endl;
    cout << "欢迎使用学生成绩管理系统" << endl;
    cout << "=================================" << endl;
    cout << "请选择操作：" << endl;
    cout << "1. 记录学生成绩" << endl;
    cout << "2. 查询学生成绩" << endl;
    cout << "3. 统计课程成绩" << endl;
    cout << "4. 退出系统" << endl;
    cout << "请输入选项序号：";
}

int main() {
    int choice;
    while (true) {
        showMenu();
        cin >> choice;

        switch (choice) {
            case 1:
                recordScore();
                break;
            case 2:
                queryScore();
                break;
            case 3:
                statScore();
                break;
            case 4:
                cout << "感谢使用学生成绩管理系统，再见！" << endl;
                return 0;
            default:
                cout << "错误：无效的选项！" << endl;
                break;
        }
    }
    return 0;
}